package com.hontech.bv.ui.fragment.heat

import android.view.View
import android.widget.Button
import com.hontech.bv.R
import com.hontech.bv.conn.api.Heat
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.window.FormDialog
import com.hontech.bv.ui.window.FormKV
import com.hontech.bv.ui.window.showProg
import com.hontech.bv.ui.wrapDialog

private fun showDropSteamDialog() {
    val dialog = FormDialog("测试出蒸汽", "出蒸汽",
        arrayOf(FormKV("时间(s)", "10"), FormKV("温度(℃)", "110"))) {
        val time = it[0]
        val temp = it[1]
        wrapDialog("出蒸汽", "出蒸汽${time}秒") {
            Heat.dropSteam(time, temp)
        }
    }
    dialog.show()
}

private fun showDropWaterDialog() {
    val dialog = FormDialog("测试出开水", "出开水",
        arrayOf(FormKV("容积(ml)", "50"))) {
        val ml = it[0]
        wrapDialog("测试出开水", "出开水${ml}ml") {
            Heat.drawWater(ml)
        }
    }
    dialog.show()
}

private fun showSetFlowDialog() {
    val dialog = FormDialog("设置流量计参数", "设置",
        arrayOf(FormKV("每升脉冲数量", "8154"))) {
        val count = it[0]
        wrapDialog("设置流量计参数", "每升脉冲数量${count}") {
            Heat.setFlow(count)
        }
    }
    dialog.show()
}

private fun showPreHeatDialog() {
    val dialog = FormDialog("预加热", "设置",
    arrayOf(FormKV("热水温度", "110"), FormKV("蒸汽温度", "140"))) {
        val water = it[0]
        val steam = it[1]
        wrapDialog("预加热", "开水:${water} 蒸汽:${steam}") {
            Heat.preHeat(0, water, steam)
        }
    }
    dialog.show()
}

private fun showStirPowderDialog() {
    val dialog = FormDialog("搅拌茶粉", "搅拌",
        arrayOf(FormKV("加水体积", "200"), FormKV("必要温度", "100"))) {
        val ml = it[0]
        val temp = it[1]
        wrapDialog("搅拌茶粉", "加水体积:${ml} 温度:${temp}") {
            Heat.stirPowder(ml, temp)
        }
    }
    dialog.show()
}

private fun showCleanDialog() {
    FormDialog("清洗", "确定", arrayOf(FormKV("清洗时间:(秒)", "10"))) {
        val sec = it[0]
        wrapDialog("清洗", "清洗${sec}秒") {
            Heat.clean(sec)
        }
    }.show()
}

private fun showPreWaterDialog() {
    FormDialog("预加水", "确定", arrayOf(
        FormKV("加水体积(ml)", "50"),
        FormKV("必要温度", "100")
    )) {
        val ml = it[0]
        val temp = it[1]
        wrapDialog("预加水", "体积:${ml} 温度:${temp}") {
            Heat.preWater(ml, temp)
        }
    }.show()
}

class CtrlDelegate (view: View) {

    private val btnStirOpen = view.findViewById<Button>(R.id.id_heat_btn_stir_open)
    private val btnStirClose = view.findViewById<Button>(R.id.id_heat_btn_stir_close)

    private val btnWaterLeft = view.findViewById<Button>(R.id.id_heat_btn_water_left)
    private val btnWaterRight = view.findViewById<Button>(R.id.id_heat_btn_water_right)

    private val btnIceLeft = view.findViewById<Button>(R.id.id_heat_btn_ice_left)
    private val btnIceRight = view.findViewById<Button>(R.id.id_heat_btn_ice_right)

    private val btnParts = view.findViewById<Button>(R.id.id_heat_btn_parts_ctrl)
    private val btnSensor = view.findViewById<Button>(R.id.id_heat_btn_sensor)
    private val btnSensor2 = view.findViewById<Button>(R.id.id_heat_btn_sensor2)

    private val btnTestSteam = view.findViewById<Button>(R.id.id_heat_btn_drop_steam)
    private val btnTestWater = view.findViewById<Button>(R.id.id_heat_btn_drop_water)
    private val btnSetFlow = view.findViewById<Button>(R.id.id_heat_btn_flow_set)
    private val btnPreHeat = view.findViewById<Button>(R.id.id_heat_btn_pre_heat)
    private val btnStirPowder = view.findViewById<Button>(R.id.id_heat_btn_stir_powder)

    private val btnDrawStart = view.findViewById<Button>(R.id.id_heat_btn_draw_start)
    private val btnDrawStop = view.findViewById<Button>(R.id.id_heat_btn_draw_stop)
    private val btnHeatStop = view.findViewById<Button>(R.id.id_heat_btn_stop_heat)


    private val btnQuery = view.findViewById<Button>(R.id.id_heat_btn_arg_query)

    private val btnClean = view.findViewById<Button>(R.id.id_heat_btn_clean)
    private val btnPreWater = view.findViewById<Button>(R.id.id_heat_btn_pre_water)

    init {
        btnClean.setOnClickListener { showCleanDialog() }
        btnPreWater.setOnClickListener { showPreWaterDialog() }

        btnStirOpen.onClick {
            wrapDialog("搅拌盒", "打开") {
                Heat.stirBoxCtrl(0)
            }
        }

        btnStirClose.onClick {
            wrapDialog("搅拌盒", "关闭") {
                Heat.stirBoxCtrl(1)
            }
        }

        btnWaterLeft.onClick {
            wrapDialog("截热水", "左边") {
                Heat.waterMotorCtrl(0)
            }
        }

        btnWaterRight.onClick {
            wrapDialog("截热水", "右边") {
                Heat.waterMotorCtrl(1)
            }
        }

        btnIceLeft.onClick {
            wrapDialog("截冰", "左边") {
                Heat.iceMotorCtrl(0)
            }
        }

        btnIceRight.onClick {
            wrapDialog("截冰", "右边") {
                Heat.iceMotorCtrl(1)
            }
        }

        btnDrawStart.onClick {
            wrapDialog("抽水控制", "启动") {
                Heat.drawCtrl(0)
            }
        }

        btnDrawStop.onClick {
            wrapDialog("抽水控制", "停止") {
                Heat.drawCtrl(1)
            }
        }

        btnHeatStop.onClick {
            wrapDialog("加热控制", "停止") {
                Heat.preHeat(1, 0, 0)
            }
        }

        btnQuery.onClick {
            val dialog = showProg("查询参数", "")

            try {
                val flow = Heat.getFlow()
                dialog.showSuccess("参数查询", "流量计每升脉冲数量:$flow")
            } catch (e: Exception) {
                e.printStackTrace()
                dialog.error(e)
            }

        }

        btnTestSteam.setOnClickListener { showDropSteamDialog() }
        btnTestWater.setOnClickListener { showDropWaterDialog() }
        btnSetFlow.setOnClickListener { showSetFlowDialog() }
        btnPreHeat.setOnClickListener { showPreHeatDialog() }
        btnStirPowder.setOnClickListener { showStirPowderDialog() }

        btnParts.setOnClickListener { HeatPartsDialog.show() }
        btnSensor.setOnClickListener { HeatSensorDialog.show() }
        btnSensor2.setOnClickListener { HeatSensor2Dialog.show() }
    }



}